package com.yanqu.road.server.cmd.travel;

import com.yanqu.road.entity.enums.eTravelEventType;
import com.yanqu.road.entity.travel.TravelEvent;
import com.yanqu.road.entity.travel.TravelSelectObjectResult;
import com.yanqu.road.entity.player.UserTravel;
import com.yanqu.road.pb.travel.TravelProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.travel.TravelModule;
import com.yanqu.road.server.manger.config.TravelMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = Protocol.S_TRAVEL_SELECT_INTERACTIVE_TYPE, desc = "选择事件对象")
public class TravelSelectInteractiveTypeCmd extends AbstractCommand {

	private int type;

	private TravelSelectObjectResult selectObjectResult;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		TravelProto.TravelSelectInteractiveTypeReqMsg reqMsg = TravelProto.TravelSelectInteractiveTypeReqMsg.parseFrom(packet.getBytes());
		type = reqMsg.getType();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		selectObjectResult = new TravelSelectObjectResult();
		TravelModule travelModule = player.getModule(TravelModule.class);
		UserTravel userTravel = travelModule.getUserTravel();
		if(null == userTravel){
			selectObjectResult.setRet(GameErrorCode.E_TRAVEL_IS_LOCK);
			return;
		}
		int eventId = userTravel.getEventId();
		if(0 == eventId){
			selectObjectResult.setRet(GameErrorCode.E_TRAVEL_NO_SPECIAL_EVENT);
			return;
		}
		TravelEvent travelEvent = TravelMgr.getTravelEvent(eventId);
		if(null == travelEvent){
			selectObjectResult.setRet(GameErrorCode.E_TRAVEL_EVENT_ERROR);
			log.info("travel select interactive type,event error,userId:{},eventId:{},select interactive type:{}", player.getUserId(), eventId, type);
			userTravel.removeEventId();
			return;
		}
		if(eTravelEventType.BrothelEvent.getValue() == travelEvent.getEventType()){
			selectObjectResult = travelModule.selectBrothelInteractiveType(travelEvent, type);
		}else if(eTravelEventType.CasinoEvent.getValue() == travelEvent.getEventType()){
			selectObjectResult = travelModule.selectCasinoValue(travelEvent, type);
		}else{
			selectObjectResult.setRet(GameErrorCode.E_TRAVEL_EVENT_ERROR);
			log.info("travel select interactive type,event error,userId:{},eventId:{},select interactive type:{}", player.getUserId(), eventId, type);
			userTravel.removeEventId();
		}
	}

	@Override
	public void sendData(GamePlayer player) {
		TravelProto.TravelSelectInteractiveTypeRespMsg.Builder respMsg = TravelProto.TravelSelectInteractiveTypeRespMsg.newBuilder();
		respMsg.setRet(selectObjectResult.getRet());
		respMsg.setEventId(selectObjectResult.getEventId());
		respMsg.setObjectId(selectObjectResult.getObjectId());
		respMsg.setAddValue(selectObjectResult.getAddValue());
		if(!StringUtils.isNullOrEmpty(selectObjectResult.getParam())) {
			respMsg.setParam(selectObjectResult.getParam());
		}
		player.sendPacket(Protocol.U_TRAVEL_SELECT_INTERACTIVE_TYPE, respMsg);
	}
}
